{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "ab0b5215",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'result': [{'request_id': '25736665_3095941'}], 'log_id': 1646921704419668}\n"
     ]
    }
   ],
   "source": [
    "# from aip import AipOcr\n",
    "\n",
    "# APP_ID = '25736665'\n",
    "# API_KEY = 'TF6r49x9GSzkMnmbvXOZGzHF'\n",
    "# SERCRET_KEY = '2q3csV1caG3ntkMjj8iQjaY7HlVapilG'\n",
    "\n",
    "# FILE = './WechatIMG91.png'\n",
    "# client = AipOcr(APP_ID, API_KEY, SERCRET_KEY)\n",
    "\n",
    "# with open(FILE, 'rb') as f:\n",
    "#     resp = client.form(f.read())\n",
    "#     print(resp)\n",
    "import requests\n",
    "\n",
    "def b64():\n",
    "    FILE = './1.jpg'\n",
    "    with open(FILE, 'rb') as f:\n",
    "        import base64\n",
    "        return base64.b64encode(f.read())\n",
    "\n",
    "img = b64()\n",
    "access_token=\"24.2e333f6f17300b7228cd3d6dee026ce0.2592000.1649511730.282335-25736665\"\n",
    "request_url = \"https://aip.baidubce.com/rest/2.0/solution/v1/form_ocr/request\"\n",
    "# 二进制方式打开图片文件\n",
    "\n",
    "params = {\"image\":img}\n",
    "request_url = request_url + \"?access_token=\" + access_token\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, data=params, headers=headers)\n",
    "if response:\n",
    "    print (response.json())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "b6a4e793",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'result': {'result_data': 'http://bj.bcebos.com/v1/ai-edgecloud/67095178774A443A9440458D2C1CD5BD.xls?authorization=bce-auth-v1%2Fd9272b4e9a38476db4470c2714e1339a%2F2022-03-10T14%3A15%3A11Z%2F172800%2F%2F28161cd76510f43ec08d4b0d97072cb2dfb9b230e3d99ed9e94b0d747576513e', 'ret_msg': '已完成', 'request_id': '25736665_3095941', 'percent': 100, 'ret_code': 3}, 'log_id': 1646921965277532}\n"
     ]
    }
   ],
   "source": [
    "url2 = 'https://aip.baidubce.com/rest/2.0/solution/v1/form_ocr/get_request_result'\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "resp = requests.post(url2+\"?access_token=\" + access_token, data={'request_id': '25736665_3095941'}, headers=headers)\n",
    "print(resp.json())"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.9.12 ('opencv')",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  },
  "vscode": {
   "interpreter": {
    "hash": "b04e532e6ce11d72ca7f2113c964d7a5531c18c5e6d3e97b1c42c6f9a3724305"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
